<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.userName" clearable class="filter-item" style="width: 200px;" placeholder="请输入用户名"/>
      <el-input v-model="listQuery.userId" clearable class="filter-item" style="width: 200px;" placeholder="请输入用户Id"/>
      <el-input v-model="listQuery.userPhone" clearable class="filter-item" style="width: 200px;" placeholder="请输入手机号"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>
	

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
     <el-table-column align="center" width="100px" label="用户ID" prop="userId" sortable/>

      <el-table-column align="center" label="用户昵称" prop="userName"/>
	  
      <el-table-column align="center" label="用户性别" prop="userSex">
        <template slot-scope="scope">
          <el-tag >{{ genderDic[scope.row.userSex] }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="用户生日" prop="userBrithday"/>
	  	 
      <el-table-column align="center" label="用户手机号码" prop="userPhone"/>
	  
	  
	   <el-table-column align="center" property="userAvatarurl" label="用户头像">
	    <template slot-scope="scope">
	      <img v-if="scope.row.user_avatarUrl" :src="scope.row.user_avatarUrl" width="40">
	    </template>
	  </el-table-column> 
      <el-table-column align="center" label="操作" width="250" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleDetail(scope.row)">详情</el-button>
					
        </template>
      </el-table-column>
	  
    </el-table>
	
	
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <!-- 用户编辑对话框 -->
    <el-dialog :visible.sync="userDialogVisible" title="用户编辑">
      <el-form ref="userDetail" :model="userDetail" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
		<el-form-item label="用户ID" prop="userId">
		  <el-input v-model="userDetail.userId" />
		</el-form-item>
        <el-form-item label="用户昵称" prop="userName">
          <el-input v-model="userDetail.userName" />
        </el-form-item>      
        <el-form-item label="用户性别" prop="userSex">
          <el-select v-model="userDetail.userSex" placeholder="请选择"><el-option v-for="(item, index) in genderDic" :key="index" :label="item" :value="index" /></el-select>
        </el-form-item>
		 <el-form-item label="用户生日" prop="userBrithday">
		  <el-input v-model="userDetail.userBrithday" />
		</el-form-item> 
		<el-form-item label="用户手机号码" prop="userPhone">
		  <el-input v-model="userDetail.userPhone" />
		</el-form-item>
		<el-form-item label="头像" prop="user_avatarUrl">
		  <el-upload
		    :action="uploadPath"
		    :show-file-list="false"
		    :on-success="uploadPicUrl"
		    class="avatar-uploader"
		    accept=".jpg,.jpeg,.png,.gif">
		    <img  width="120px" v-if="userDetail.user_avatarUrl" :src="userDetail.user_avatarUrl" class="avatar" >
		    <i v-else class="el-icon-plus avatar-uploader-icon " />
		  </el-upload>
		</el-form-item>
      </el-form>
	  
		  
      <div slot="footer" class="dialog-fxooter">
        <el-button @click="userDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleUserUpdate">确定</el-button>
      </div>
	  
    </el-dialog>
	
  </div>
  
</template>

<script>
import { fetchList ,userDetail ,updateUser, uploadPath } from '@/api/user'
import Pagination from '@/components/Pagination' 

export default {
  name: 'User',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
	  uploadPath,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        userName: undefined,
        mobile: undefined,
		userPhone:undefined,
        userId: undefined,
        sort: 'add_time',
        order: 'desc'
      },
	  
      downloadLoading: false,
      genderDic: ['未知', '男', '女'],
      statusDic: ['可用', '禁用', '注销'],
      userDialogVisible: false,
      userDetail:{
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
	  
	
    getList() {
      this.listLoading = true

      fetchList(this.listQuery).then(response => {
      	
        this.list = response.data.data
      console.log(this.list)
        this.listLoading = false
      }).catch(() => {
      	
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    uploadPicUrl:function(res){
    	console.log(res)
    	this.userDetail.userAvatarurl = res.data
    	console.log(this.userDetail.userAvatarurl)
    },
		handleDetail(row) {
		  this.userDetail = row
		  this.userDialogVisible = true
		},
    
    handleUserUpdate(){
     updateUser(this.userDetail)
	 
        .then((response) => {
          this.userDialogVisible = false
          this.$notify.success({
            title: '成功',
            message: '更新用户成功'
          })
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
    }
  }
}
</script>
